﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using JiraConnector;
using System.Windows.Forms;

namespace ProjectExcel2007AddIn
{
    public partial class RibbonProjectBridge
    {
        private void RibbonProjectBridge_Load(object sender, RibbonUIEventArgs e)
        {
            loadSettings(false);
        }

        private void btnImport_Click(object sender, RibbonControlEventArgs e)
        {
            try
            {
                //Test add one issue to the current project 
                Microsoft.Office.Interop.Excel.Application app = Globals.ThisAddIn.Application;
         

                JIRAIssueList list = JiraConnector.JIRAConnector.getJIRAIssues(
                          Properties.Settings.Default.JIRA_Username,
                          Properties.Settings.Default.JIRA_Password,
                          Properties.Settings.Default.JIRA_URL);


                app.Cells[1, 1] = "key";
                app.Cells[1, 2] = "summary";
                app.Cells[1, 3] = "aggregatetimeestimate";
                app.Cells[1, 4] = "aggregatetimespent";
                app.Cells[1, 5] = "startDate";
                app.Cells[1, 6] = "dueDate";
                app.Cells[1, 7] = "hyperlink";
                app.Cells[1, 8] = "assignee";

                int i = 1; 
                foreach (JIRAIssue issue in list.issues)
                {
                    i++;
                    app.Cells[i, 1] = issue.key;
                    app.Cells[i, 2] = issue.fields["summary"];
                    app.Cells[i, 3] = issue.aggregatetimeestimate() * 60;
                    app.Cells[i, 4] = issue.aggregatetimespent() * 60;
                    app.Cells[i, 5] = issue.startDate();
                    app.Cells[i, 6] = issue.dueDate();
                    app.Cells[i, 7] = issue.hyperlink();
                    app.Cells[i, 8] = issue.assignee();
                }
            }
            catch (System.Exception ex)
            {
                System.Console.Write("Error:" + ex.Message);
                MessageBox.Show("An error happened:" + ex.Message,"Error");
            }
        }

   
        private void btnSettings_Click(object sender, RibbonControlEventArgs e)
        {
            loadSettings(true);
        }

        private static void loadSettings(bool showDialog)
        {
            JiraConnector.Settings s = new JiraConnector.Settings();
            if (showDialog)
                s.ShowDialog();

            Properties.Settings.Default.JIRA_Username = s.JIRA_Username();
            Properties.Settings.Default.JIRA_Password = s.JIRA_Password();
            Properties.Settings.Default.JIRA_URL = s.JIRA_URL();

        }

        private void btnPreview_Click(object sender, RibbonControlEventArgs e)
        {
            JiraConnector.Main s = new JiraConnector.Main();
            s.ShowDialog();
        }
    }
}
